#include "thread_sync.h"
#include "log_module.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// 共享资源
int counter = 0;

// 锁的定义
pthread_mutex_t mutex_lock;
pthread_rwlock_t rw_lock;
pthread_spinlock_t spin_lock;
pthread_cond_t cond_var;
pthread_mutex_t cond_mutex;
sem_t semaphore;

// 条件变量标志
int condition_met = 0;

// 初始化所有锁
void init_locks() {
    pthread_mutex_init(&mutex_lock, NULL);
    pthread_rwlock_init(&rw_lock, NULL);
    pthread_spin_init(&spin_lock, PTHREAD_PROCESS_PRIVATE);
    pthread_cond_init(&cond_var, NULL);
    pthread_mutex_init(&cond_mutex, NULL);
    sem_init(&semaphore, 0, 1);
}

// 销毁所有锁
void destroy_locks() {
    pthread_mutex_destroy(&mutex_lock);
    pthread_rwlock_destroy(&rw_lock);
    pthread_spin_destroy(&spin_lock);
    pthread_cond_destroy(&cond_var);
    pthread_mutex_destroy(&cond_mutex);
    sem_destroy(&semaphore);
}

// 互斥锁线程函数
void *mutex_thread(void *arg) {
    pthread_mutex_lock(&mutex_lock);
    counter += 1;
    LOG_INFO("互斥锁线程: counter = %d\n", counter);
    pthread_mutex_unlock(&mutex_lock);
    return NULL;
}

// 读写锁线程（读）函数
void *rwlock_thread_read(void *arg) {
    pthread_rwlock_rdlock(&rw_lock);
    LOG_INFO("读写锁线程（读）: counter = %d\n", counter);
    pthread_rwlock_unlock(&rw_lock);
    return NULL;
}

// 读写锁线程（写）函数
void *rwlock_thread_write(void *arg) {
    pthread_rwlock_wrlock(&rw_lock);
    counter += 2;
    LOG_INFO("读写锁线程（写）: counter = %d\n", counter);
    pthread_rwlock_unlock(&rw_lock);
    return NULL;
}

// 自旋锁线程函数
void *spinlock_thread(void *arg) {
    pthread_spin_lock(&spin_lock);
    counter += 3;
    LOG_INFO("自旋锁线程: counter = %d\n", counter);
    pthread_spin_unlock(&spin_lock);
    return NULL;
}

// 条件变量线程函数
void *cond_var_thread(void *arg) {
    pthread_mutex_lock(&cond_mutex);
    while (!condition_met) {
        pthread_cond_wait(&cond_var, &cond_mutex);
    }
    counter += 4;
    LOG_INFO("条件变量线程: counter = %d\n", counter);
    pthread_mutex_unlock(&cond_mutex);
    return NULL;
}

// 信号量线程函数
void *semaphore_thread(void *arg) {
    LOG_INFO("信号量: semaphore = %d\n", semaphore);
    sem_wait(&semaphore);
    counter += 5;
    LOG_INFO("信号量线程: counter = %d\n", counter);
    sem_post(&semaphore);
    LOG_INFO("信号量: semaphore = %d\n", semaphore);
    return NULL;
}

// 信号条件发送线程
void *signal_condition_thread(void *arg) {
    sleep(1); // 模拟某种条件满足的延迟
    pthread_mutex_lock(&cond_mutex);
    condition_met = 1;
    pthread_cond_signal(&cond_var);
    LOG_INFO("条件变量信号发送线程：条件已满足\n");
    pthread_mutex_unlock(&cond_mutex);
    return NULL;
}

int locks_verify() {
    pthread_t threads[9];

    // 初始化所有锁
    init_locks();

    // 创建线程并验证各种锁的效果
    pthread_create(&threads[0], NULL, mutex_thread, NULL);
    pthread_create(&threads[1], NULL, rwlock_thread_read, NULL);
    pthread_create(&threads[2], NULL, rwlock_thread_write, NULL);
    pthread_create(&threads[3], NULL, spinlock_thread, NULL);
    pthread_create(&threads[4], NULL, cond_var_thread, NULL);
    pthread_create(&threads[5], NULL, semaphore_thread, NULL);
    pthread_create(&threads[6], NULL, semaphore_thread, NULL); // 多个线程使用信号量
    pthread_create(&threads[7], NULL, signal_condition_thread, NULL);
    pthread_create(&threads[8], NULL, rwlock_thread_read, NULL);

    // 等待所有线程完成
    for (int i = 0; i < 9; i++) {
        pthread_join(threads[i], NULL);
    }

    // 销毁所有锁
    destroy_locks();

    LOG_INFO("最终 counter 值：%d\n", counter);
    return 0;
}
